package heven.holt.wan.android.repository

import heven.holt.wan.android.api.ApiWanNavigationServer
import heven.holt.wan.android.model.BaseModel
import heven.holt.wan.android.model.NavigationModel
import heven.holt.wan.android.model.ProjectModel

class WanNavigationRepository(
    private val apiWanNavigationServer: ApiWanNavigationServer
) {
    suspend fun getNavigationList(): BaseModel<List<NavigationModel>> =
        apiWanNavigationServer.navigation()

    suspend fun getProjectList(): BaseModel<List<ProjectModel>> =
        apiWanNavigationServer.projects()

    fun getProjectModelByCid(projectList: List<ProjectModel>, cid: Int): ProjectModel? {
        projectList.forEach { projectModel ->
            val find = projectModel.children?.find { it.id == cid }
            if (find != null) return projectModel
        }
        return null
    }

    fun getPositionByProjectTree(projectModel: ProjectModel, cid: Int): Int? {
        val find = projectModel.children?.find { it.id == cid }
        return projectModel.children?.indexOf(find)
    }
}